package cn.sczhyt.modules.app.controller;

import cn.sczhyt.common.utils.PageUtils;
import cn.sczhyt.common.utils.R;
import cn.sczhyt.modules.app.annotation.Login;
import cn.sczhyt.modules.app.annotation.LoginUser;
import cn.sczhyt.modules.app.entity.UserEntity;
import cn.sczhyt.modules.szhy.entity.MeetingVoteEntity;
import cn.sczhyt.modules.szhy.service.MeetingVoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 表决表
 *
 * @author luoshijie
 * @email 1169414426@qq.com
 * @date 2020-11-25 10:54:22
 */
@Api(tags = "APP表决功能")
@RestController
@RequestMapping("app/meetingvote")
public class AppMeetingVoteController {
    @Autowired
    private MeetingVoteService meetingVoteService;

    /**
     * 列表
     */

    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = meetingVoteService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 表决结果
     */
    @ApiOperation("表决结果")
    @RequestMapping("/voteCount")
    public R voteCount(@RequestParam String meetingId, @RequestParam String topicsId) {

        JSONObject jsonObject = meetingVoteService.voteCount(meetingId, topicsId);

        return R.ok().put("data", jsonObject);
    }

    /**
     * 表决结果列表
     */
    @ApiOperation("表决名单详情")
    @RequestMapping("/voteList")
    public R voteList(@RequestParam String meetingId,
                      @RequestParam String topicsId) {

        List<MeetingVoteEntity> list = meetingVoteService.findByTopicsId(meetingId, topicsId);

        return R.ok().put("data", list);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") String id){
		MeetingVoteEntity meetingVote = meetingVoteService.getById(id);

        return R.ok().put("meetingVote", meetingVote);
    }

    /**
     * 保存
     */
    @Login
    @ApiOperation("表决功能")
    @RequestMapping("/vote")
    public R vote(@LoginUser UserEntity user,
                  @RequestBody MeetingVoteEntity meetingVote){

        meetingVote.setUserId(user.getUserId());
        meetingVote.setUserName(user.getUsername());
        meetingVote.setVoteTime(new Date());

        return  meetingVoteService.saveVote(meetingVote);
    }



}
